package com.aistar.order.controller;




import com.aistar.common.vo.Result;
import com.aistar.common.vo.StatusCode;

import com.aistar.order.util.SMSCodes;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


/**
 * Created by js on 2019/6/24.
 */
@RestController
public class PhoneController {

    @Autowired
    private SMSCodes smsCode;

    //发送验证码
    @GetMapping("/phoneor/{check}/{phone}")
    public Result message(@PathVariable("check") String check,@PathVariable("phone")String phone,HttpSession session){
        String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");
        System.out.println(checkcode_server);


        if(!checkcode_server.equalsIgnoreCase(check)){
            System.out.println("验证码错误");
             return new Result(false,5000,"验证码错误");
        }else{

            Result result=null;
            System.out.println(phone);

            //产生一个随机的手机验证码
            String code= RandomStringUtils.randomNumeric(4);
            try {
                smsCode.sendCode(phone,code);
            } catch (Exception e) {
                e.printStackTrace();
            }
            result=new Result(true,StatusCode.OK,"已发送",code);
            System.out.println(result.getCode());
//            session.removeAttribute(check); //防止验证码复用
            return result;
        }


        }





}